package persistence;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import entity.Funcionario;

public class FuncionarioDao {
	
	Session session;
	Transaction transaction;
	Query query;
	Criteria criteria;
	
	public void cadastrarFunc(Funcionario f) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.save(f);
		transaction.commit();
		session.close();		
	}
	
	public List<Funcionario> listarFunc() throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		List<Funcionario> listaFunc = new ArrayList<Funcionario>();
		criteria = session.createCriteria(Funcionario.class);
		criteria.addOrder(Order.asc("nome"));
		listaFunc = criteria.list();
		session.close();
		return listaFunc;
	}
	
	public void editarFunc(Funcionario f) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.update(f);
		transaction.commit();
		session.close();
	}
	
	public void excluirFunc(Funcionario f) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.delete(f);
		transaction.commit();
		session.close();
	}
	
	

}
